package com.example.demo.Controller;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.config.AppConfig;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Article;
import com.example.demo.model.Board;
import com.example.demo.model.User;
import com.example.demo.services.IArticleReplyService;
import com.example.demo.services.IArticleService;
import com.example.demo.services.IBoardService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
    @RequestMapping("/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;


    @Resource
    private IArticleReplyService articleReplyService;



    @RequestMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("boardId") Long boardId,
                            @RequestParam("title") String title,
                            @RequestParam("content") String content){
        //查看用户是否被禁言
        HttpSession session=request.getSession();
        User user=(User)session.getAttribute(AppConfig.USER_SESSION_KEY);

        //用户禁言
        if(user.getState()==1){
            //返回异常
            return AppResult.failed(ResultCode.FAILED_USER_BANNED.getMessage());
        }

        //查看板块
        Board board=boardService.selectById(boardId);
        if(board == null || board.getState() == 1 || board.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //创建文章
        Article article=new Article();
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        article.setTitle(title);
        article.setContent(content);
        articleService.create(article);

        //返回成功
        return AppResult.success();
    }


    @RequestMapping("/getAll")
    public AppResult<List<Article>> getAll(@RequestParam(value = "boardId" , required = false) Long boardId) {
         //查看板块
        List<Article> articles=articleService.selectAll();
        if(articles == null){
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);
    }


    @RequestMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@RequestParam(value = "boardId"  )Long boardId) {
       List<Article> articles;
        if(boardId == null){
            //查看板块
            articles=articleService.selectAll();
            if(articles == null){
                articles = new ArrayList<>();
            }
        }else{
            articles = articleService.selectByBoardId(boardId);
        }

        return AppResult.success(articles);
    }


    //获取板块信息
    @RequestMapping("/getById")
    public AppResult<Board> getById(@RequestParam(value = "id") @NonNull Long id){
        //查看板块
        Board board = boardService.selectById(id);
        // 判断板块是否存在
        if(board == null || board.getState() == 1 ){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }

//    @ApiOperation("根据帖子Id获取详情")
//    @RequestMapping("/details")
//    public AppResult<Article> getDetails( @RequestParam(value = "id") @NonNull Long id){
//        Article article=articleService.selectDetailById(id);
//        if(article ==null){
//            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
//        }
//
//        return AppResult.success(article);
//    }


    @ApiOperation("根据帖子Id获取详情 ")
    @RequestMapping("/details")
    public AppResult<Article> getDetails( HttpServletRequest request,
                                          @RequestParam(value = "id") @NonNull Long id){

        HttpSession session=request.getSession();
        User user= (User) session.getAttribute(AppConfig.USER_SESSION_KEY);

        Article article=articleService.selectDetailById(id);
        if(article ==null){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        if(user.getId().equals(article.getUserId())) {
            article.setOwn(true);
        }

        return AppResult.success(article);
    }


    @ApiOperation("修改帖子")
    @RequestMapping("/modify")
    public AppResult modify(HttpServletRequest request ,
                            @RequestParam(value = "id") Long id ,
                            @RequestParam(value = "title")String title,
                            @RequestParam(value = "content")String content){
        //获取用户信息
        HttpSession session=request.getSession();
        User user=(User)session.getAttribute(AppConfig.USER_SESSION_KEY);
        //查看用户状态
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取文章
        Article article=articleService.selectById(id);

        //检查title是否为空
        if(article==null ){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        //判断用户是不是作者
        if(!user.getId().equals(article.getUserId())){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        //修改
        articleService.modify(id,title,content);
        return AppResult.success();

    }

    @ApiOperation("点赞")
    @RequestMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,
                                  @RequestParam(value = "id") Long id) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        //判断用户是否为空
        if(user ==null){
            return  AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    @ApiOperation("删除帖子")
    @RequestMapping("/deleteById")
    public AppResult  deleteById( HttpServletRequest request,
                                  @RequestParam(value="id") Long id){
        HttpSession session=request.getSession();
        User user=(User)session.getAttribute(AppConfig.USER_SESSION_KEY);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        Article article= articleService.selectById(user.getId());
        if(article==null ){
            System.out.println(article.getDeleteState());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        if(!user.getId().equals(article.getUserId())){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        articleService.deleteById(id);
        return AppResult.success();
    }

//    @ApiOperation("回复帖子")
//    @RequestMapping("/create")
//    public AppResult create(HttpServletRequest request,
//                           @RequestParam(value = "articleId") Long articleId,
//                           @RequestParam(value = "content")  String content){
//        //获取用户信息
//        HttpSession session=request.getSession();
//        User user=(User)session.getAttribute(AppConfig.USER_SESSION_KEY);
//
//        //查看用户状态
//        if(user.getState() !=0 ){
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
//        }
//
//
//        //获取文章
//        Article article =articleService.selectById(articleId);
//        if(article==null){
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
//        }
//        if(article.getState() !=0){
//            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
//        }
//
//        //创建回复
//        ArticleReply articleReply=new ArticleReply();
//        articleReply.setArticleId(articleId);
//        articleReply.setPostUserId(user.getId());
//        articleReply.setContent(content);
//        articleReply.setReplyId(null);
//
//        //调用dao
//        articleReplyService.create(articleReply);
//
//        return AppResult.success();
//    }


}
